/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.handler.voiced;

import l2s.commons.configuration.Config;
import l2s.gameserver.cache.HtmCache;
import l2s.gameserver.handler.IVoicedCommandHandler;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.network.SystemMessageId;
import l2s.gameserver.network.serverpackets.NpcHtmlMessage;

/**
 * Developers: Silentium Team
 * Author: Demon
 * Date: 18/12/2013
 * Time: 21:57:01
 */

public class Menu implements IVoicedCommandHandler
{
	private final String[] _voicedCommands =
	{
		"menu",
		"m_menu_"
	};
	
	@Override
	public boolean useVoicedCommand(final String command, final L2PcInstance activeChar, final String target)
	{
		if (command.startsWith("menu"))
		{
			showHtm(activeChar);
		}
		else if (command.startsWith("m_menu_"))
		{
			String addcmd = command.substring(7).trim();
			
			if (addcmd.startsWith("autoloot"))
			{
				int flag = Integer.parseInt(addcmd.substring(8).trim());
				if (flag == 0)
				{
					activeChar.setAutoLoot(false);
					activeChar.sendMessage("Автолут отключен.");
				}
				else
				{
					activeChar.setAutoLoot(true);
					activeChar.sendMessage("Автолут включен.");
				}
				showHtm(activeChar);
				return true;
			}
			else if (addcmd.startsWith("exp"))
			{
				int flag = Integer.parseInt(addcmd.substring(3).trim());
				if (flag == 0)
				{
					activeChar.setStopExp(true);
					activeChar.sendMessage("You can not gain experience by killing mobs.");
				}
				else
				{
					activeChar.setStopExp(false);
					activeChar.sendMessage("You can gain experience by killing mobs.");
				}
				showHtm(activeChar);
				return true;
			}
			else if (addcmd.startsWith("pm"))
			{
				int flag = Integer.parseInt(addcmd.substring(2).trim());
				if (flag == 0)
				{
					activeChar.setInRefusalMode(false);
					activeChar.sendPacket(SystemMessageId.MESSAGE_ACCEPTANCE_MODE);
				}
				else
				{
					activeChar.setInRefusalMode(true);
					activeChar.sendPacket(SystemMessageId.MESSAGE_REFUSAL_MODE);
				}
				showHtm(activeChar);
				return true;
			}
			else if (addcmd.startsWith("offline"))
			{
				if (((Config.OFFLINE_TRADE_ENABLE) && ((activeChar.getPrivateStoreType() == 1) || (activeChar.getPrivateStoreType() == 3) || (activeChar.getPrivateStoreType() == 8))) || ((Config.OFFLINE_CRAFT_ENABLE) && (activeChar.getPrivateStoreType() == 5)))
				{
					activeChar.store();
					activeChar.closeNetConnection(true);
				}
				else
				{
					activeChar.sendMessage("Вы не можете использовать оффлайн торговлю/крафт.");
					showHtm(activeChar);
				}
				return true;
			}
			else
			{
				showHtm(activeChar);
			}
		}
		
		return false;
	}
	
	private static void showHtm(L2PcInstance activeChar)
	{
		NpcHtmlMessage htm = new NpcHtmlMessage(activeChar.getLastQuestNpcObject());
		String text = HtmCache.getInstance().getHtm("data/html-en/menu/menu.htm");
		htm.setHtml(text);
		activeChar.sendPacket(htm);
		
		if (activeChar.getStopExp())
		{
			htm.replace("%gainexp%", "OFF");
		}
		else
		{
			htm.replace("%gainexp%", "ON");
		}
		
		if (activeChar.isAutoLoot())
		{
			htm.replace("%autoloot%", "ON");
		}
		else
		{
			htm.replace("%autoloot%", "OFF");
		}
		
		if (activeChar.isInRefusalMode())
		{
			htm.replace("%pm%", "OFF");
		}
		else
		{
			htm.replace("%pm%", "ON");
		}
	}
	
	@Override
	public String[] getVoicedCommandList()
	{
		return _voicedCommands;
	}
}